<?php
namespace cms\modules\v1\controllers;

use cms\modules\v1\models\access\ModuleActions;
use cms\modules\v1\models\userFund\RechargeOrder;
use cms\modules\v1\models\userFund\RechargeSetting;
use common\models\Functions;
use common\models\Status;
use Yii;
use yii\filters\auth\HttpHeaderAuth;
use yii\helpers\ArrayHelper;

/**
 * Default controller for the `v1` module
 */
class RechargeController extends DefaultController
{

    public $modelClass = '';

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            'authenticator' => [
                'class' => HttpHeaderAuth::className(),    //head头部验证
                'header' => 'access-token',
                'optional' => [                     //需要过滤掉不用token参数的方法
                ]
            ]
        ]);
    }

    /* ====================== 充值设置 ========================= */
    public function actionSettingList($page = 1, $size = 20) {
        if (Yii::$app->request->isGet) {
            return (new RechargeSetting())->getListData($page, $size);
        }
    }

    public function actionSettingAddEdit($id = 0) {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        $model = new RechargeSetting();
        if (Yii::$app->request->isGet) {
            $list =  $model->getInfo($id);
            return $list;
        }

        if (Yii::$app->request->isPost) {
            $model->setScenario('addEdit');
            if ($model->load(Yii::$app->request->post(), '')) {
                return $model->addEdit();
            }
        }
    }

    //切换启用禁用状态
    public function actionSettingSwitchStatus() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isPost) {
            return (new RechargeSetting())->switchStatus();
        }
    }


    /* ====================== 充值订单 ========================= */
    public function actionList($page = 1, $size = 20) {
        if (Yii::$app->request->isGet) {
            return (new RechargeOrder())->getListData($page, $size);
        }
    }

    public function actionTypeMap() {
        if (Yii::$app->request->isGet) {
            return Functions::formatRestAPI(0, '', Status::PAY_WAY_MAP);
        }
    }

    public function actionStatusMap() {
        if (Yii::$app->request->isGet) {
            return Functions::formatRestAPI(0, '', Status::RECHARGE_STATUS_MAP);
        }
    }
}
